<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<c:if test="${userID==null}">
  <c:redirect url="index.jsp" />
</c:if>

<c:if test="${userType=='G'}">
  <c:redirect url="main.jsp" />
</c:if>

<c:if test="${pageContext.request.method=='POST'}">
  <c:if test="${param.cancel!=null}">
    <c:redirect url="main.jsp" />
  </c:if>

  <sql:query var="messages" dataSource="${dataSource}">
  select max(c_number)+1 as msgnum from t_messages where c_forum_code = ? 
  <sql:param value="${param.id}" />
  </sql:query>

  <c:set var="msgnum" value="0" />

  <c:forEach var="row" items="${messages.rows}">
    <c:if test="${row.msgnum!=null}">
      <c:set var="msgnum" value="${row.msgnum}" />
    </c:if>
  </c:forEach>

  <sql:update dataSource="${dataSource}">
  insert into t_messages(c_forum_code,c_number,c_posted,c_subject,c_sender,c_message) values(?,?,now(),?,?,?) 
  <sql:param value="${param.id}" />
  <sql:param value="${msgnum}" />
  <sql:param value="${param.subject}" />
  <sql:param value="${userID}" />
  <sql:param value="${param.msg}" />
  </sql:update>

  <c:url value="forum.jsp" var="rtn">
    <c:param name="id" value="${param.id}" />
  </c:url>

  <c:redirect url="${rtn}" />
</c:if>

<html>
  <head>
    <title>
    <fmt:message key="post.title" bundle="${lang}"/></title>
  </head>

  <body>
    <sql:query var="forums" dataSource="${dataSource}">
    select c_code,c_name from t_forums where c_code = ? 
    <sql:param value="${param.id}" />
    </sql:query>

    <c:forEach var="row" items="${forums.rows}">
      <form method="post">
      <h3><fmt:message key="post.ins" bundle="${lang}"/> 
      <c:out value="${row.c_name}(${row.c_code})" />

      <input type="hidden" name="id" value="<c:out value="${param.id}"/>"/></h3>

      <fmt:message key="forum.subject" bundle="${lang}"/>
      <input type="text" name="subject" width="60" size="20" />

      <br />

      <textarea rows="10" cols="40" name="msg">
      </textarea>

      <br />

      <input type="submit" name="send" value="<fmt:message key="button.ok" bundle="${lang}"/>" />

      <input type="submit" name="cancel" value="<fmt:message key="button.cancel" bundle="${lang}"/>" />
      </form>
    </c:forEach>
  </body>
</html>

